package org.dey.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.CheckGroupMapper;
import org.dey.mapper.CheckItemMapper;
import org.dey.pojo.CheckGroup;
import org.dey.pojo.CheckItem;
import org.dey.pojo.PageVo;
import org.dey.service.CheckGroupService;
import org.dey.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author hg
 * @date 2022/3/15
 * @classname
 */
@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {

    @Autowired
    CheckItemMapper checkItemMapper;

    @Autowired
    CheckGroupService checkGroupService;

    @Override
    public PageVo<CheckItem> checkItemXx(CheckItem checkItem ,int pageNum, int pagesize) {



        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>();
        if(StringUtils.isNotBlank(checkItem.getName())){
            queryWrapper.like("name",checkItem.getName());
        }

       PageHelper.startPage(pageNum, pagesize);

        List<CheckItem> list = this.list(queryWrapper);

        PageInfo<CheckItem> pageInfo = new PageInfo<>(list);


        PageVo<CheckItem> pageVo = new PageVo<CheckItem>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;


    }


    @Override
    public Result zjCheckItemXx(CheckItem checkItem) {
        //添加检查项时先查询数据库 检查项表最后一条数据 得到该检查项的编码

        Result result =null;

        CheckItem checkItem1 = checkItemMapper.getCheckItem();

        String strcode = "";


         if(checkItem1.getCode()!=null&&checkItem1.getCode()!=""){
             String code = (Integer.parseInt(checkItem1.getCode())+1)+"";
             strcode = StringUtils.leftPad(code, 4, "0");

         }

        checkItem.setCode(strcode);

        boolean save = this.save(checkItem);

        if(save){
            result = new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return result;
    }

    @Override
    public Result scCheckItemXx(String code) {

        Result result =null;

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code",code);
        boolean b = this.remove(queryWrapper);
        if(b){
            result = new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return result;


    }

    @Override
    public Result xgCheckItemXx(CheckItem checkItem) {

        Result result =null;

        UpdateWrapper<CheckItem> updateWrapper = new UpdateWrapper<CheckItem>();
        updateWrapper.eq("code",checkItem.getCode())
                .set("name",checkItem.getName())
                .set("sex",checkItem.getSex())
                .set("age",checkItem.getAge())
                .set("price",checkItem.getPrice())
                .set("type",checkItem.getType())
                .set("attention",checkItem.getAttention())
                .set("remark",checkItem.getRemark());

        boolean update = this.update(updateWrapper);
        if(update){
            result = new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_CHECKITEM_FAIL);
        }

        return result;
    }

    @Override
    public CheckItem cxCheckItemXxBYcode(String code) {

        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>();
        queryWrapper.eq("code",code);
        CheckItem query = this.getOne(queryWrapper);
        return query;
    }

    @Override
    public List<CheckItem> checkItemXxqb(int id) {

        boolean update = false;
        //查询前先清空数据
        int xc = checkItemMapper.xc(id);
        //清除数据后还原检查组价格为0
        if(xc>=0){
            UpdateWrapper<CheckGroup> updateWrapper = new UpdateWrapper<CheckGroup>();
            updateWrapper.eq("id",id).set("price",0);
           update = checkGroupService.update(updateWrapper);
        }

        List<CheckItem> list = null;
        if(update){
            if(xc>=0){
                QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<CheckItem>();

                queryWrapper.select("id","name","price");

                list = this.list(queryWrapper);
            }
        }
        return list;
    }
}
